"""
Case Type   : cm_ctl
Case Name   : 执行cm_ctl pause后,执行cm_ctl set/reload
Create At   : 2023/03/24
Owner       : opentestcase012
Description :
    1.执行cm_ctl pause;
    2.在主节点执行set,修改主节点的参数值,reload生效;
    3.kill数据库主节点;
    4.查看cm_agent下的cm_agent-*-current.log日志;
    5.恢复参数值并执行reload生效;
    6.恢复集群状态;
Expect      :
    1.暂停成功
    2.参数值修改成功
    3.kill成功
    4.主节点日志中不打印log级别日志
    5.恢复成功
    6.恢复成功
History     :
"""
import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


# @unittest.skipIf(2 != primary_sh.get_node_num(), '非1+2环境不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.constant = Constant()
        self.pri_user = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pause_file = os.path.join(macro.GAUSSHOME_BIN_PATH,
                                       'cluster_manual_pause')

    def test_cm(self):
        text = '----step1:执行cm_ctl pause; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl('pause')
        self.assertIn(self.constant.cm_pause_success_msg, cm_ctl_res,
                      '执行失败:' + text)

        text = '----step2:在主节点执行set,修改主节点的参数值,reload生效; expect:参数值修改成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl_set(
            'agent', 'log_min_messages=fatal')
        self.assertTrue(cm_ctl_res, '执行失败:' + text)

        text = '----查看cm_agent日志中log日志; expect:成功----'
        self.log.info(text)
        grep_cmd = f"cd {macro.CMA_LOG_PATH};" \
            f"grep -c 'StartAndStop LOG: DN START' cm_agent-*-current.log"
        log_count_old = self.common.get_sh_result(self.pri_user, grep_cmd)

        text = '-----step3:kill数据库主节点; expect:kill成功-----'
        self.log.info(text)
        kill_cmd = '''ps ux|grep "gaussdb -D"|grep -v grep|\
        tr -s ' '|cut -d ' ' -f 2|xargs kill -9;'''
        kill_res = self.common.get_sh_result(self.pri_user, kill_cmd)
        self.assertEqual('', kill_res, '执行失败' + text)

        text = '----step4:查看cm_agent下的cm_agent-*-current.log日志; ' \
               'expect:主节点日志中不打印log级别日志----'
        self.log.info(text)
        grep_cmd = f"cd {macro.CMA_LOG_PATH};" \
            f"grep -c 'StartAndStop LOG: DN START' cm_agent-*-current.log"
        log_count_new = self.common.get_sh_result(self.pri_user, grep_cmd)
        self.assertTrue(int(log_count_new) - int(log_count_old) == 0,
                        "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '----step5:恢复参数值并执行reload生效; expect:恢复成功----'
        self.log.info(text)
        cm_ctl_res = self.primary_sh.exec_cm_ctl_set(
            'agent', 'log_min_messages=warning')

        text = '----step6:恢复集群状态; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res1 = self.primary_sh.exec_cm_ctl('resume')
        cm_ctl_res2 = self.primary_sh.exec_cm_ctl('start')
        cm_ctl_res3 = self.primary_sh.exec_cm_ctl('switchover', '-a')
        time.sleep(10)
        cm_ctl_res4 = self.primary_sh.exec_cm_ctl('query', '-Civ')
        self.assertTrue(cm_ctl_res, '执行失败:' + text)
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res1,
                      '执行失败:' + text)
        self.assertIn(self.constant.cm_start_success_msg, cm_ctl_res2,
                      '执行失败:' + text)
        self.assertIn(self.constant.cm_switchover_success_msg, cm_ctl_res3,
                      '执行失败:' + text)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res4,
                      "执行失败:" + text)
        self.log.info(f'----{os.path.basename(__file__)} finish----')
